const http = require('http');
const fs = require('fs');
const path = require('path')
const url = require('url')
const common = require('./module/common')

http.createServer(function (request, response) {
  
  // 1、获取地址
  let pathName = url.parse(request.url).pathname
  pathName = pathName === '/' ? '/index.html' : pathName

  // 2、获取后缀名paht.extname()
  let extname = path.extname(pathName)

  // 3、通过fs模块读取文件
  if (pathName !== '/favicon.ico') {
    fs.readFile('./static' + pathName, (err, data) => {
      if (err) {
        common.setRes(response, 404, 'text/html', '喔唷，你访问的页面丢失了...')
      }
      // 获取请求头
      let mime = common.getFileMime(extname)
      common.setRes(response, 200, mime, data)
    })
  }
  

}).listen(8081);